﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using NotebookS2.Reports.Spreadsheet;
using DevExpress.XtraSplashForm;
using DevExpress.XtraWaitForm;



namespace NotebookS2.Reports.Pivot
{
    public partial class PivotSalesAgentCustDoc : Form
    {
        public PivotSalesAgentCustDoc()
        {
            InitializeComponent();
        }

        public string ConnectionString { get; set; }

        public void PrintPreview()
        {
            pivotGridControl1.ShowPrintPreview();
        }

        public void BindGrid()
        {
            using (SqlConnection oConnection = new SqlConnection(this.ConnectionString))
            {
                oConnection.Open();

                SqlCommand oCommand = new SqlCommand();
                SqlDataAdapter oAdapter = new SqlDataAdapter();
                DataSet ds = new System.Data.DataSet();

                oCommand.Connection = oConnection;
                oCommand.CommandType = System.Data.CommandType.StoredProcedure;
                oCommand.CommandText = "SP_SALES_DOC_AGENT_CUSTOMER";
                oCommand.Parameters.Add(new SqlParameter("@FROM", Convert.ToDateTime(dtFROM.EditValue)));
                oCommand.Parameters.Add(new SqlParameter("@TO", Convert.ToDateTime(dtTO.EditValue)));

                oAdapter.SelectCommand = oCommand;
                oAdapter.Fill(ds);

                pivotGridControl1.DataSource = ds.Tables[0];
                pivotGridControl1.Refresh();


            }
        }

        private void PivotSalesAgentCustDoc_Load(object sender, EventArgs e)
        {

            dtFROM.EditValue = System.DateTime.Now.ToShortDateString();
            dtTO.EditValue = System.DateTime.Now.ToShortDateString();
        }

        private void btnRetrieve_Click(object sender, EventArgs e)
        {
            DevExpress.XtraSplashScreen.SplashScreenManager.ShowForm(typeof(WaitForm1));
            BindGrid();
            DevExpress.XtraSplashScreen.SplashScreenManager.CloseForm();
        }

        private void btnOpenInExcel_Click(object sender, EventArgs e)
        {
            if (Directory.Exists(System.Windows.Forms.Application.StartupPath.ToString() + "\\Temp") == false)
            {
                Directory.CreateDirectory(System.Windows.Forms.Application.StartupPath.ToString() + "\\Temp");
            }

            string sXcelFileName = System.Windows.Forms.Application.StartupPath.ToString() + "\\Temp\\" + System.DateTime.Now.ToString("MMddyyyhhss") + ".xlsx";
            pivotGridControl1.ExportToXlsx(sXcelFileName);

            Process.Start(sXcelFileName);
        }
    }
}
